﻿using System;
using System.Collections.Generic;

using Kiiro.Common.Interfaces;
using Kiiro.Common.Utility;

namespace Kiiro.Common.Entities
{
    [Serializable]
    public class Work : BaseEntity, IEntity
    {
        #region Private Properties

        private DateTime startDate;
        private List<ValidationMessage> startDateMessage = new List<ValidationMessage>();
        private DateTime endDate;
        private List<ValidationMessage> endDateMessage = new List<ValidationMessage>();
        private float assignedWork;
        private List<ValidationMessage> assignedWorkMessage = new List<ValidationMessage>();
        private float percentageComplete;
        private List<ValidationMessage> percentageCompleteMessage = new List<ValidationMessage>();
        private string statusNote;
        private List<ValidationMessage> statusNoteMessage = new List<ValidationMessage>();
        private List<int> assignedToList = new List<int>();
        private List<ValidationMessage> assignedToListMessage = new List<ValidationMessage>();
        private string parentTasks;
        private List<ValidationMessage> parentTasksMessage = new List<ValidationMessage>();
        private bool isHighPriority;
        private List<ValidationMessage> isHighPriorityMessage = new List<ValidationMessage>();
        private bool isHighPriorityAdd = false;
        private bool isHighPriorityDel = false;
        private WorkType workItemType;
        private List<ValidationMessage> workItemTypeMessage = new List<ValidationMessage>();
        private StatusType status;
        private List<ValidationMessage> statusMessage = new List<ValidationMessage>();
        private string membersEmail;
        private List<ValidationMessage> membersEmailMessage = new List<ValidationMessage>();
        private string projectManagersEmail;
        private List<ValidationMessage> projectManagersEmailMessage = new List<ValidationMessage>();        
        private bool isLate;
        private bool isLateAdd = false;
        private bool isLateDel = false;
        private bool isSlipping;
        private bool isSlippingAdd = false;
        private bool isSlippingDel = false;
        private bool isFromProjectPlan;
        private bool isDirty;
        private int planId;

        private float beforeAssignedWork;
        private DateTime beforeStart;
        private DateTime beforeEnd;
        private StatusType beforeStatus;
        private float beforePercentComplete;
        private List<int> BeforeAssignedToList;

        #endregion

        #region Public Properties

        public DateTime StartDate
        {
            get{return startDate;}
            set
            {
                if(startDate != value)
                {
                    startDate = value;
                    UpdateEntity();
                }
            }
        }

        public List<ValidationMessage> StartDateMessage
        {
            get{return startDateMessage;}
            set
            {
                if(startDateMessage != value)
                {
                    startDateMessage = value;
                    UpdateEntity();
                }
            }
        }

        public DateTime EndDate
        {
            get{return endDate;}
            set
            {
                if (endDate != value)
                {
                    endDate = value;
                    UpdateEntity();
                }
            }
        }

        public List<ValidationMessage> EndDateMessage
        {
            get{return endDateMessage;}
            set
            {
                if (endDateMessage != value)
                {
                    endDateMessage = value;
                    UpdateEntity();
                }
            }
        }

        public float AssignedWork
        {
            get{return assignedWork;}
            set
            {
                if(assignedWork != value)
                {
                    assignedWork = value;
                    UpdateEntity();
                }
            }
        }

        public List<ValidationMessage> AssignedWorkMessage
        {
            get{return assignedWorkMessage;}
            set
            {
                if(assignedWorkMessage != value)
                {
                    assignedWorkMessage = value;
                    UpdateEntity();
                }
            }
        }

        public float PercentageComplete
        {
            get{return percentageComplete;}
            set
            {
                if(percentageComplete != value)
                {
                    percentageComplete = value;
                    UpdateEntity();
                }
            }
        }

        public List<ValidationMessage> PercentageCompleteMessage
        {
            get{return percentageCompleteMessage;}
            set
            {
                if(percentageCompleteMessage != value)
                {
                    percentageCompleteMessage = value;
                    UpdateEntity();
                }
            }
        }

        public string StatusNote
        {
            get{return statusNote;}
            set
            {
                if(statusNote != value)
                {
                    statusNote = value;
                    UpdateEntity();
                }
            }
        }

        public List<ValidationMessage> StatusNoteMessage
        {
            get{return statusNoteMessage;}
            set
            {
                if(statusNoteMessage != value)
                {
                    statusNoteMessage = value;
                    UpdateEntity();
                }
            }
        }

        public List<int> AssignedToList
        {
            get { return assignedToList; }
            set
            {
                if (assignedToList != value)
                {
                    assignedToList = value;
                    UpdateEntity();
                }
            }
        }

        public List<ValidationMessage> AssignedToListMessage
        {
            get { return assignedToListMessage; }
            set
            {
                if (assignedToListMessage != value)
                {
                    assignedToListMessage = value;
                    UpdateEntity();
                }
            }
        }

        public string ParentTasks
        {
            get { return parentTasks; }
            set
            {
                if (parentTasks != value)
                {
                    parentTasks = value;
                    UpdateEntity();
                }
            }
        }

        public List<ValidationMessage> ParentTasksMessage
        {
            get { return parentTasksMessage; }
            set
            {
                if (parentTasksMessage != value)
                {
                    parentTasksMessage = value;
                    UpdateEntity();
                }
            }
        }

        public bool IsHighPriority
        {
            get{return isHighPriority;}
            set
            {
                if(isHighPriority != value)
                {
                    isHighPriority = value;
                    UpdateEntity();
                }
            }
        }

        public List<ValidationMessage> IsHighPriorityMessage
        {
            get{return isHighPriorityMessage;}
            set
            {
                if(isHighPriorityMessage != value)
                {
                    isHighPriorityMessage = value;
                    UpdateEntity();
                }
            }
        }

        /// <summary>
        /// Used to indicate that Issue is newly High Priority and to update Project Statistics
        /// </summary>
        public bool IsHighPriorityAdd
        {
            get { return this.isHighPriorityAdd; }
            set { this.isHighPriorityAdd = value; }
        }

        /// <summary>
        /// Used to indicate that Issue is no longer High Priority and to update Project Statistics
        /// </summary>
        public bool IsHighPriorityDel
        {
            get { return this.isHighPriorityDel; }
            set { this.isHighPriorityDel = value; }
        }

        public WorkType WorkItemType
        {
            get{return workItemType;}
            set
            {
                if(workItemType != value)
                {
                    workItemType = value;
                    UpdateEntity();
                }
            }
        }

        public List<ValidationMessage> WorkItemTypeMessage
        {
            get{return workItemTypeMessage;}
            set
            {
                if(workItemTypeMessage != value)
                {
                    workItemTypeMessage = value;
                    UpdateEntity();
                }
            }
        }

        public StatusType Status
        {
            get{return status;}
            set
            {
                if(status != value)
                {
                    status = value;
                    UpdateEntity();
                }
            }
        }

        public List<ValidationMessage> StatusMessage
        {
            get { return statusMessage; }
            set
            {
                if ( statusMessage != value )
                {
                    statusMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public bool IsLate
        {
            get{return isLate;}
            set
            {
                if(isLate != value)
                {
                    isLate = value;
                    UpdateEntity();
                }
            }
        }

        /// <summary>
        /// Used to indicate that Work item is newly Late and to update Project Statistics
        /// </summary>
        public bool IsLateAdd
        {
            get { return this.isLateAdd; }
            set { this.isLateAdd = value; }
        }

        /// <summary>
        /// Used to indicate that Work item is no longer Late and to update Project Statistics
        /// </summary>
        public bool IsLateDel
        {
            get { return this.isLateDel; }
            set { this.isLateDel = value; }
        }

        public bool IsSlipping
        {
            get{return isSlipping;}
            set
            {
                if(isSlipping != value)
                {
                    isSlipping = value;
                    UpdateEntity();
                }
            }
        }

        /// <summary>
        /// Used to indicate that Task is newly Slipping and to update Project Statistics
        /// </summary>
        public bool IsSlippingAdd
        {
            get { return this.isSlippingAdd; }
            set { this.isSlippingAdd = value; }
        }

        /// <summary>
        /// Used to indicate that Task is no longer Slipping and to update Project Statistics
        /// </summary>
        public bool IsSlippingDel
        {
            get { return this.isSlippingDel; }
            set { this.isSlippingDel = value; }
        }

        public bool IsFromProjectPlan
        {
            get{return isFromProjectPlan;}
            set
            {
                if(isFromProjectPlan != value)
                {
                    isFromProjectPlan = value;
                    UpdateEntity();
                }
            }
        }

        public bool IsDirty
        {
            get{return isDirty;}
            set
            {
                if(isDirty != value)
                {
                    isDirty = value;
                    UpdateEntity();
                }
            }
        }

        public int PlanId
        {
            get { return planId; }
            set
            {
                if (planId != value)
                {
                    planId = value;
                    UpdateEntity();
                }
            }
        }
        
        public float BeforeAssignedWork
        {
            get { return beforeAssignedWork; }
            set { beforeAssignedWork = value; }
        }

        public DateTime BeforeStart
        {
            get { return beforeStart; }
            set { beforeStart = value; }
        }

        public DateTime BeforeEnd
        {
            get { return beforeEnd; }
            set { beforeEnd = value; }
        }

        public StatusType BeforeStatus
        {
            get { return beforeStatus; }
            set { beforeStatus = value; }
        }
        
        public float BeforePercentComplete
        {
            get { return beforePercentComplete; }
            set { beforePercentComplete = value; }
        }

        public List<int> BeforeAssignedToList1
        {
            get { return BeforeAssignedToList; }
            set { BeforeAssignedToList = value; }
        }

        
        public string MembersEmail
        {
            get { return membersEmail; }
            set
            {
                if (membersEmail != value)
                {
                    membersEmail = value;
                    UpdateEntity();
                }
            }
        }

        public List<ValidationMessage> MembersEmailMessage
        {
            get { return membersEmailMessage; }
            set
            {
                if (membersEmailMessage != value)
                {
                    membersEmailMessage = value;
                    UpdateEntity();
                }
            }
        }


        public string ProjectManagersEmail
        {
            get { return projectManagersEmail; }
            set
            {
                if (projectManagersEmail != value)
                {
                    projectManagersEmail = value;
                    UpdateEntity();
                }
            }
        }

        public List<ValidationMessage> ProjectManagersEmailMessage
        {
            get { return projectManagersEmailMessage; }
            set
            {
                if (projectManagersEmailMessage != value)
                {
                    projectManagersEmailMessage = value;
                    UpdateEntity();
                }
            }
        }

        #endregion

        #region IEntity Members
        public new bool IsValid()
        {
            return AllValidationErrors().Count == 0;
        }

        public new List<ValidationMessage> AllValidationErrors()
        {
            List<ValidationMessage> allErrors = new List<ValidationMessage>();

            allErrors.AddRange ( TitleMessage );
            allErrors.AddRange(startDateMessage);
            allErrors.AddRange(endDateMessage);
            allErrors.AddRange(assignedWorkMessage);
            allErrors.AddRange(percentageCompleteMessage);
            allErrors.AddRange(statusNoteMessage);
            allErrors.AddRange(assignedToListMessage);
            allErrors.AddRange(parentTasksMessage);
            allErrors.AddRange(isHighPriorityMessage);
            allErrors.AddRange ( workItemTypeMessage );
            allErrors.AddRange(DescriptionMessage);
            allErrors.AddRange(AttachmentListMessage);

            return allErrors;
        }

        public new void ClearValidationMessages()
        {
            TitleMessage.Clear ( );
            startDateMessage.Clear();
            endDateMessage.Clear();
            assignedWorkMessage.Clear();
            percentageCompleteMessage.Clear();
            statusNoteMessage.Clear();
            assignedToListMessage.Clear();
            parentTasksMessage.Clear();
            isHighPriorityMessage.Clear();
            workItemTypeMessage.Clear();
            DescriptionMessage.Clear();
            AttachmentListMessage.Clear();
        }

        #endregion

        /// <summary>
        /// Search predicate returns true if a work item is a Task
        /// </summary>
        /// <param name="work"></param>
        /// <returns></returns>
        public static bool IsTask ( Work work )
        {
            return work.WorkItemType == WorkType.Task;
        }

        /// <summary>
        /// Search predicate returns true if a work item is an Issue
        /// </summary>
        /// <param name="work"></param>
        /// <returns></returns>
        public static bool IsIssue ( Work work )
        {
            return work.WorkItemType == WorkType.Issue;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Common/Entities/Work.cs $
 * 
 * 8     12/14/09 2:10p Shafia
 * attachments file extensions validation for discussions, tasks, and
 * issues
 * 
 * 7     11/09/09 4:09p Shafia
 * 
 * 6     17/08/09 10:38a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/